﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*----------------------------------------------------------------
* 描述 ：
* 作    者 ：Tengsheng Hou
* 更新时间 ：2019/7/23 11:21:08
//----------------------------------------------------------------*/
namespace Core.Toolkit.Authorization.Help
{
    public class PermissionHelp
    {
        private Type _type;
        public PermissionHelp(Type type)
        {
            this._type = type;
        }
        public PermissionHelp(object obj)
        {
            if (obj != null)
                this._type = obj.GetType();
            else
                new CustomOperationException("使用PermissionHelp帮助类不能传入null");
        }
        public List<string> GetFunctionCode()
        {
            List<string> functionCodes = new List<string>();
            var methodList = _type.GetMethods().Where(
                   m => m.GetCustomAttributes(typeof(PermissionFilter), true).Any()
               );
            foreach (var item in methodList)
            {
                var attr = item.GetCustomAttributes(typeof(PermissionFilter), true).FirstOrDefault();
                PermissionFilter permissionFilter = (PermissionFilter)attr;
                functionCodes.Add(permissionFilter.FunctionCode);
            }
            return functionCodes;
        }

        public List<PermissionFilter> GetPermissionAttribute()
        {
            List<PermissionFilter> list = new List<PermissionFilter>();
            var methodList = _type.GetMethods().Where(
                   m => m.GetCustomAttributes(typeof(PermissionFilter), true).Any()
               );
            foreach (var item in methodList)
            {
                var attr = item.GetCustomAttributes(typeof(PermissionFilter), true).FirstOrDefault();
                PermissionFilter permissionFilter = (PermissionFilter)attr;
                list.Add(permissionFilter);
            }
            return list;
        }
    }
}
